﻿using Corina.DataContext.Service;
using Corina.Datamodel.DataObject;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Corina.DataContext.Master
{
    public class UserMaster : MasterBase
    {
        public ResultInfo Login(string account,string password)
        {
            ResultValue value = VerifyAccount<UserInfo>(account, password);

            ResultInfo result = new ResultInfo(value);

            if (value == ResultValue.Succeed)
            {
                UserInfo user = GetAccountInfo<UserInfo>(account);

                UserLevelInfo level = GetUserLevel(account);

                AvatarInfo avatar = GetAccountAvatar(user.Id, AccountTypeValue.User);

                result.Insert(user, level, avatar);
            }

            result.Result = value;

            return result;
        }

        public ResultInfo GetPendingUser(string account, string password)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                var table = GetInstance<UserService>().GetPendingUser(ConnectionString(DatabaseKeys.CORINA_ACCO));
                string[] columns = table.Columns.OfType<DataColumn>().Select(t => t.ColumnName).ToArray();

                var items = (from t in table.AsEnumerable()
                             select new DataObjectInfo()
                             {
                                 Values = GetObjectOfValue(t, columns)
                             });

                result.DataObjects.AddRange(items);
            }

            return result;
        }

        public ResultInfo UpdateUserStatus(string account, string password, int userId, AccountStatusValue status)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            if (resultValue == ResultValue.Succeed)
            {
                bool flag = GetInstance<UserService>().UpdateStatus(ConnectionString(DatabaseKeys.CORINA_ACCO), userId, status);
                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
            }

            return new ResultInfo(resultValue);
        }

        public ResultInfo GetAllItems(string account, string password, string key, int index, int size = 20)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                int count = 0;
                var table = GetInstance<UserService>().GetAllItems(ConnectionString(DatabaseKeys.CORINA_ACCO), key, index, size, ref count);
                string[] columns = table.Columns.OfType<DataColumn>().Select(t => t.ColumnName).ToArray();

                var items = (from t in table.AsEnumerable()
                             select new DataObjectInfo()
                             {
                                 Values = GetObjectOfValue(t, columns)
                             });

                result.DataObjects.AddRange(items);
                result.TotalCount = count;
            }

            return result;
        }

        public ResultInfo Register(string account, string password, string name, string mobile, string email, string linkman, int kindId, int industryId)
        {
            ResultValue resultValue = ResultValue.Unknown;
            if (ExistsAccount<UserInfo>(account))
            {
                resultValue = ResultValue.Exists;
            }
            else
            {
                UserInfo entity = new UserInfo()
                {
                    Account = account,
                    Password = password,
                    Email = email,
                    Name = name,
                    Mobile = mobile,
                    Linkman = linkman,
                    UserKindId = kindId,
                    IndustryId = industryId
                };

                bool flag = GetInstance<UserService>().Update(ConnectionString(DatabaseKeys.CORINA_ACCO), entity, EntityOperation.Insert);
                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
                else
                {
                    resultValue = ResultValue.Succeed;
                }
            }

            return new ResultInfo(resultValue);
        }

        public ResultInfo Register(UserInfo entity)
        {
            ResultValue resultValue = ResultValue.Unknown;
            if (ExistsAccount<UserInfo>(entity.Account))
            {
                resultValue = ResultValue.Exists;
            }
            else
            {

                bool flag = GetInstance<UserService>().Update(ConnectionString(DatabaseKeys.CORINA_ACCO), entity, EntityOperation.Insert);
                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
                else
                {
                    resultValue = ResultValue.Succeed;
                }
            }

            return new ResultInfo(resultValue);
        }

        public ResultInfo ChangeUserLevel(string account, string password, int userId, int levelId)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            if (resultValue == ResultValue.Succeed)
            {
                bool flag = GetInstance<UserService>().ChangeUserLevel(ConnectionString(DatabaseKeys.CORINA_ACCO), userId, levelId);
                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
            }

            return new ResultInfo(resultValue);
        }

        public ResultInfo UpdatePassword(string account, string password, string newpassword)
        {
            ResultValue resultValue = VerifyAccount<UserInfo>(account, password);

            if (resultValue == ResultValue.Succeed)
            {
                bool flag = GetInstance<UserService>().UpdatePassword(ConnectionString(DatabaseKeys.CORINA_ACCO), account, newpassword);
                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
            }

            return new ResultInfo(resultValue);
        }

        public ResultInfo Update(string account, string password, UserInfo entity, EntityOperation operation)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            if (resultValue == ResultValue.Succeed)
            {
                bool flag = GetInstance<UserService>().Update(ConnectionString(DatabaseKeys.CORINA_ACCO), entity, operation);
                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
            }

            return new ResultInfo(resultValue);
        }

        public ResultInfo Update(string account, string password, string name, string mobile, string email, string linkman, int industry)
        {
            ResultValue resultValue = VerifyAccount<UserInfo>(account, password);

            if (resultValue == ResultValue.Succeed)
            {
                UserInfo entity = GetAccountInfo<UserInfo>(account);

                entity.Name = name;
                entity.Mobile = mobile;
                entity.Email = email;
                entity.Linkman = linkman;
                entity.IndustryId = industry;

                bool flag = GetInstance<UserService>().Update(ConnectionString(DatabaseKeys.CORINA_ACCO), entity, EntityOperation.Update);
                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
            }

            return new ResultInfo(resultValue);
        }

        /// <summary>
        /// 获取用户信息（用户）
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public ResultInfo GetUserInfo(string account, string password)
        {
            ResultValue resultValue = VerifyAccount<UserInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                var item = GetAccountInfo<UserInfo>(account);
                result.Entities.Add(item);
            }

            return result;
        }

        /// <summary>
        /// 获取用户信息（管理员）
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public ResultInfo GetUserInfo(string account, string password, int userId)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                var item = GetAccountInfo<UserInfo>(userId);
                result.Entities.Add(item);
            }

            return result;
        }
    }
}
